package com.hk.service.impl.kyc;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.hk.commons.*;
import com.hk.commons.vo.ActivityRes;
import com.hk.commons.vo.PushMsg;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.hk.commons.entity.Activity;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.kyc.IActivity;

@Service
public class ActivityImpl extends BaseServiceImpl<Activity> implements IActivity {

	@Value("${push.url.licaishi}")
	private String url_lcs;

	@Value("${push.url.huacaitong}")
	private String url_hct;

	@Value("${push.env}")
	private String env;

	@Value("${push.tag}")
	private String tag;

	@Override
	public Boolean pushActMsg(int id, int channel){
		ExecutorService executor = Executors.newFixedThreadPool(1);

		Runnable task = new Runnable() {
			public void run() {
				System.out.println("env = " + env );
				Activity act =  findById(id);
				PushMsg pushMsg = new PushMsg();
				pushMsg.setEnv(env);
				pushMsg.setMenu("activity");
				pushMsg.setIsShare(act.getIsShare());
				pushMsg.setMsg(act.getActDesc());
				pushMsg.setTitle(act.getName());
				pushMsg.setUuid(act.getId().toString() + System.currentTimeMillis());
				pushMsg.setUrl(act.getUrl());
				if(StringHelper.isNotEmpty(tag)){
					pushMsg.setTags(tag);
				}
				pushMsg.setChannel(channel);
				pushMsg.setMsgTypeConstants("9");

				if(channel == 1){
					HttpUtils.pushMsgToUser(url_hct, pushMsg);
				}else if(channel == 2 ){
					HttpUtils.pushMsgToUser(url_lcs, pushMsg);
				}

			}
		};
		executor.execute(task);
		executor.shutdown();

		return true;
	}







	@Override
	public void read(Integer id, Long userId) {
		if (!isRead(id, userId)) {
			RedisCilent.sadd(Constant.ACTIVITY + id, userId.toString());

		}
	}

	@Override
	public void readAll(Integer channel, Long userId) {
		List<Activity> activityList = this.getAllByChannel(channel);
		for(Activity act : activityList){
			this.read(act.getId(), userId);
		}
	}

    @Override
    public void readAllByActivityList(List<LinkedHashMap> activityResList, Long userId) {
        for (LinkedHashMap act : activityResList) {
            this.read((Integer) act.get("id"), userId);
        }
    }

	@Override
	public boolean isRead(Integer id, Long userId) {
		Boolean result = false;
		if(RedisCilent.existsKey(Constant.ACTIVITY+id)) {
			result = RedisCilent.hasValue(Constant.ACTIVITY + id, userId.toString());
		}
		return result;
	}

	@Override
	public Activity findById(Integer id) {
		Activity result = null;
		if (id != null){
			result = getById(id);
		}
		return result;
	}

	@Override
	public Boolean add(Activity model) {
		 
		model.setCts(System.currentTimeMillis());
		super.save(model);
		return true;
	}
	
	@Override
	public Boolean isExist(Integer id){
		Activity activity = findById(id);
		
		
		return activity == null?false:true;
	}
	
	@Override
	public List<Activity> getActiveActivity(){
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("status", Constant.ACTIVITY_ACTIVE);
		List<Activity> actList = findByFilter(hqlFilter);

		return actList;
	}

	@Override
	public List<Activity> getAllByChannel(int channel){
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("status", Constant.ACTIVITY_ACTIVE);
		hqlFilter.addLKFilter("channel", channel);
		List<Activity> actList = findByFilter(hqlFilter);
		return actList;
	}

	@Override
	public List<Activity> getActiveActivity(int channel, int curPage, int pageSize){
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("status", Constant.ACTIVITY_ACTIVE);
		hqlFilter.addLKFilter("channel", channel);
		hqlFilter.addSort("cts");
		hqlFilter.addOrder("desc");
		List<Activity> actList = findByFilter(hqlFilter, curPage, pageSize);
		return actList;
	}

	public ActivityRes getRes(Activity act){
		ActivityRes activityRes = new ActivityRes();
		ReqToModelHelper.copyReqValueToModel(act, activityRes);

		if(act.getStartStatus() != null) {
			String startStatus = "";
			int status = act.getStartStatus();
			if(status == 1) {
				startStatus = "预热中";
			}else if(status == 2) {
				startStatus = "进行中";
			}else if( status == 3) {
				startStatus = "已结束";
			}else{
				startStatus = "已结束";
			}

			activityRes.setStartStatusStr(startStatus);

		}

		if(act.getStartTime() != null){
			Long startTime = act.getStartTime().getTime();
			activityRes.setStartTimeStr(DateUtil.longToShortDate(startTime));
		}

		if(act.getEndTime() != null){
			Long endTime = act.getEndTime().getTime();
			activityRes.setEndTimeStr(DateUtil.longToShortDate(endTime));
		}

		return activityRes;
	}

	public ActivityRes getRes(Activity act, Long userId){
		ActivityRes activityRes = getRes(act);

		if(userId != null){
			Boolean isRead = false;
			if(userId != null) {
				isRead = isRead(activityRes.getId(), userId);
				activityRes.setRead(isRead);
			}
		}


		return activityRes;
	}


	@Override
	public List<ActivityRes> getResByChannel(Integer channel) {

		List<ActivityRes> actReslist = new ArrayList<>();

		List<Activity> list = getAllByChannel(channel);

		if(list == null || list.size() == 0){
			return null;
		}

		for (Activity ag : list) {
			ActivityRes activityRes =  getRes(ag);
			actReslist.add(activityRes);
		}
		return actReslist;
	}

	@Override
	public List<Activity> page(int pageNo, int pageSize, List<Activity> list){


		pageNo = pageNo - 1;

		if(pageNo < 0){
			pageNo = 0;
		}

		List<Activity> result = new ArrayList<Activity>();
		if(list != null && list.size() > 0){
			int allCount = list.size();

			int pageCount = (allCount + pageSize-1) / pageSize; //总页数

			if(pageNo >= pageCount){
				pageNo = pageCount;
			}
			int start = pageNo * pageSize;

			int end = start +  pageSize;

			if(end >= allCount){
				end = allCount;
			}
			for(int i = start; i < end; i ++){
				result.add(list.get(i));
			}
		}

		return (result != null && result.size() > 0) ? result : null;
	}

	@Override
	public List<Activity> getResByChannelAndLocation(Integer channel, String location, Long userId) {

		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("status", Constant.ACTIVITY_ACTIVE);
		hqlFilter.addLKFilter("location", location);
		hqlFilter.addLKFilter("channel", channel);

		if(location.equals( Constant.ACTIVITY_LOCATION_BANANER)){
			hqlFilter.addSql(" and startStatus != " + Constant.ACTIVITY_START_STATUS_END );
		}

		List<Activity> list = findByFilter(hqlFilter);

		return list;
	}
}
